﻿Public Class UserManagementSettingsControl
    Private Sub UserControl_DataContextChanged_1(sender As Object, e As DependencyPropertyChangedEventArgs)
        If TypeOf DataContext Is UserManagementViewModel Then
            Dim vm As UserManagementViewModel = DataContext
            If vm.EditAllowed Then
                UserCreationTab.Visibility = Windows.Visibility.Visible
            Else
                UserCreationTab.Visibility = Windows.Visibility.Collapsed
            End If
        End If
    End Sub

    Public Sub New()
        ' Dieser Aufruf ist für den Designer erforderlich.
        InitializeComponent()

        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.
        UserCreationTab.DataContext = New NewUserModel
    End Sub

    Private Sub FocusTBParentForMVVM(textbox As TextBox)
        Dim ancestor As DependencyObject = textbox.Parent
        While ancestor IsNot Nothing
            Dim element = TryCast(ancestor, UIElement)
            If element IsNot Nothing AndAlso element.Focusable Then
                element.Focus()
                Exit While
            End If
            ancestor = VisualTreeHelper.GetParent(ancestor)
        End While
    End Sub

    Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs)
        If TypeOf DataContext Is UserManagementViewModel AndAlso TypeOf UserCreationTab.DataContext Is NewUserModel Then
            FocusTBParentForMVVM(UserNameTB)
            Dim vm As UserManagementViewModel = DataContext
            Dim usr As NewUserModel = UserCreationTab.DataContext
            usr.Password = PasswordBox.Password
            vm.AddNewUser(usr.Name, usr.ID, usr.Password, usr.CanReadOrder, usr.CanWriteOrder, usr.CanReadArticle, usr.CanWriteArticle, usr.CanReadCustomer, usr.CanWriteCustomer, usr.CanReadDatabase, usr.CanWriteDatabase, usr.CanEditUsers)
            'Console.WriteLine(cb1.DataContext)
            'If  Then
            '    UserCreationTab.DataContext = New NewUserModel
            '    PasswordBox.Password = ""
            '    SaveFailedLbl.Visibility = Windows.Visibility.Collapsed
            'Else
            '    SaveFailedLbl.Visibility = Windows.Visibility.Visible
            'End If
        End If
    End Sub

    Private Sub UserNameTB_TextChanged(sender As Object, e As TextChangedEventArgs) Handles UserNameTB.TextChanged
        If TypeOf DataContext Is UserManagementViewModel Then
            Dim vm As UserManagementViewModel = DataContext
            If vm.UserExists(UserNameTB.Text) Then
                UserNameTB.Foreground = System.Windows.Media.Brushes.Red
            Else
                UserNameTB.Foreground = System.Windows.Media.Brushes.Black
            End If
        End If
    End Sub

    Private Sub UserIDTB_TextChanged(sender As Object, e As TextChangedEventArgs) Handles UserIDTB.TextChanged
        If TypeOf DataContext Is UserManagementViewModel Then
            Dim vm As UserManagementViewModel = DataContext
            If vm.UserIDExists(UserIDTB.Text) Then
                UserIDTB.Foreground = System.Windows.Media.Brushes.Red
            Else
                UserIDTB.Foreground = System.Windows.Media.Brushes.Black
            End If
        End If
    End Sub
End Class
